﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IOBusiness;
using System.Data;
using System.Text;

namespace Indochinaodyssey.UserControl.NavigatorMenu
{
    public partial class TopMenuVertical : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            lblMes.Text = TopMenuCountry();
        }

        private string TopMenuCountry()
        {
            string sqlQuocGia = "Select ID,TenQuocGia from tblQuocGia";
            clsBSDungChung bs = new clsBSDungChung();
            DataTable tbCountry = new DataTable();
            tbCountry = bs.GetDataTableSQL(sqlQuocGia, "QuocGia");
            StringBuilder menu = new StringBuilder();
            menu.Append("<ul class=\"sf-menu\">");
                menu.Append("<li class=\"current\">");
                menu.Append("<a class=\"link\" href=\"/Default.aspx\" style=\"font-weight:bold;\">HOME</a>");
                menu.Append("<ul>");
                menu.Append("</ul>");
                menu.Append("</li>");
            for (int i = 0; i < tbCountry.Rows.Count; i++)
            {
                menu.Append("<li class=\"current\">");
                menu.Append("<a href=\"/Default.aspx?country=" + tbCountry.Rows[i][0].ToString() + " \" style=\"font-weight:bold;\">" + tbCountry.Rows[i][1].ToString() + "</a>");
                menu.Append("<ul>");
                menu.Append(TopMenuCategory(tbCountry.Rows[i][0].ToString()));
                menu.Append("</ul>");
                menu.Append("</li>");
            }
            menu.Append("</ul>");
            return menu.ToString();
        }
        private string TopMenuCategory(string countryID)
        {
            string sql = "SELECT t.TenDanhMuc, t.ID,t.TypeID FROM dbo.tblDanhMuc t  where t.ID in(Select l.maDanhmuc from tblTheLoai l where l.maquocgia=" + countryID + " group by l.MaDanhMuc)";
            clsBSDungChung bs = new clsBSDungChung();
            DataTable tbCat = new DataTable();
            tbCat = bs.GetDataTableSQL(sql, "Cate");
            StringBuilder menu = new StringBuilder();
            
            
            for (int i = 0; i < tbCat.Rows.Count; i++)
            {
                menu.Append("<li class=\"current\">");
                if (Convert.ToInt32(tbCat.Rows[i][2].ToString()) != 1)
                {
                    menu.Append("<a href=\"/TourList.aspx?country=" + countryID + "&sub=" + tbCat.Rows[i][1].ToString() + "\">" + tbCat.Rows[i][0].ToString() + "</a>");
                }
                else
                {
                    menu.Append("<a href=\"/Infomation.aspx?country=" + countryID + "&loaiid=" + tbCat.Rows[i][1].ToString() + "\">" + tbCat.Rows[i][0].ToString() + "</a>");
                }
                menu.Append("<ul>");
                menu.Append(TopMenuSubCategory(countryID,tbCat.Rows[i][1].ToString(),tbCat.Rows[i][2].ToString()));
                menu.Append("</ul>");
                menu.Append("</li>");
            }
            
            return menu.ToString();
        }
        private string TopMenuSubCategory(string countryID,string catID,string type)
        {
            string sql = "Select ID,TenTheLoai from tblTheLoai where MaDanhMuc="+catID+" and MaQuocGia="+countryID;
            string sql2 = "Select t.tieude,t.id from tblInfomation t,tblTheLoai tt,tblDanhMuc d where t.TheLoaiID=tt.Id and tt.madanhmuc=d.id and tt.maquocgia=" + countryID;
            clsBSDungChung bs = new clsBSDungChung();
            DataTable tbCat = new DataTable();
            DataTable tbInfo = new DataTable();
            tbCat = bs.GetDataTableSQL(sql, "SubCategory");
            tbInfo = bs.GetDataTableSQL(sql2, "Info");
            StringBuilder menu = new StringBuilder();
            if (type=="1")
            {
                for (int i = 0; i < tbInfo.Rows.Count; i++)
                {
                        menu.Append("<li class=\"current\">");
                        menu.Append("<a href=\"/InfomationDetail.aspx?country=" + countryID + "&sub=" + catID + "&id=" + tbInfo.Rows[i][1].ToString() + " \">" + tbInfo.Rows[i][0].ToString() + "</a>");
                        menu.Append("</li>");
                }
            }
            else if (type=="2")
            {
                for (int i = 0; i < tbCat.Rows.Count; i++)
                {
                    menu.Append("<li class=\"current\">");                 
                    menu.Append("<a href=\"/Tour.aspx?country=" + countryID + "&sub=" + catID + "&cateid=" + tbCat.Rows[i][0].ToString() + " \">" + tbCat.Rows[i][1].ToString() + "</a>");
                    menu.Append("</li>");
                }
            }
           

            return menu.ToString();
        }
    }
}